package com.diandi.activity.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by 袁恩光 on 2018/3/21.
 */
@RestController
public class LoginController {

    /**
     * shiro 登录拦截 和 登录请求 进入DbRealm
     */
    @RequestMapping(value = "login", method = RequestMethod.GET)
    public ModelAndView login() {
        return new ModelAndView("login");
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ModelAndView fail() {
        Subject currentUser = SecurityUtils.getSubject();
        if(currentUser.isAuthenticated()){
            return new ModelAndView("index");
        }
        ModelAndView modelAndView = new ModelAndView("login");
        modelAndView.addObject("error", "用户名或者密码错误");
        return modelAndView;
    }

    @RequestMapping("/")
    public ModelAndView loginSuccess() {
        return new ModelAndView("index");
    }

    @RequestMapping(value = "/logout")
    public ModelAndView logout() {
        Subject currentUser = SecurityUtils.getSubject();
        currentUser.logout();
        return new ModelAndView("login");
    }

}
